/*
 * PrivateChatFrame.java
 *
 * Created on Mar 20, 2011, 3:53:54 PM
 */
package clientFrame;

import common.FileTransferMessage;
import javax.swing.JFileChooser;

/**
 *
 * @author Martina Willig
 */
public class PrivateChatFrame extends javax.swing.JFrame {

    private String username;
    private ClientInterface clientInterface;
    private Boolean block = false;

    /** Creates new form PrivateChatFrame */
    public PrivateChatFrame(String username, ClientInterface parent) {
        this.username = username;
        this.clientInterface = parent;
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jBtnSend = new javax.swing.JButton();
        jTextFieldWrite = new javax.swing.JTextField();
        jScrollPane1 = new javax.swing.JScrollPane();
        jMessageArea = new javax.swing.JTextArea();
        jBtnSelectFile = new javax.swing.JButton();
        jlblInfo = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jBtnSend.setText("Send");
        jBtnSend.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBtnSendActionPerformed(evt);
            }
        });

        jTextFieldWrite.setText("jTextField1");

        jMessageArea.setColumns(20);
        jMessageArea.setRows(5);
        jScrollPane1.setViewportView(jMessageArea);

        jBtnSelectFile.setText("File...");
        jBtnSelectFile.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBtnSelectFileActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 558, Short.MAX_VALUE))
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addGap(53, 53, 53)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jlblInfo)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jTextFieldWrite, javax.swing.GroupLayout.DEFAULT_SIZE, 410, Short.MAX_VALUE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jBtnSend)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jBtnSelectFile)))))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 223, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jTextFieldWrite, javax.swing.GroupLayout.PREFERRED_SIZE, 29, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jBtnSelectFile)
                    .addComponent(jBtnSend))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 24, Short.MAX_VALUE)
                .addComponent(jlblInfo)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jBtnSelectFileActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBtnSelectFileActionPerformed
       
        JFileChooser c = new JFileChooser();
        int rVal = c.showOpenDialog(this);
        if (rVal == JFileChooser.APPROVE_OPTION) {
            String filename = c.getSelectedFile().getName();
            String dir = c.getCurrentDirectory().toString();
            String filepath = c.getSelectedFile().getAbsolutePath();
            this.jlblInfo.setText(filename);
                try {
                    FileTransferMessage msg = new FileTransferMessage(filepath, username);
                    //getClientInterface().getClient().sendToServer(msg.getParsedCommandString());
                    getClientInterface().getClient().sendToServer("SEND---" + getUsername().trim() + "---file---" + msg.getContent());
                    
                if (!jMessageArea.getText().equals("")) {
                    this.getjMessageArea().setText(getjMessageArea().getText() + getUsername() + " sends :\n" + filename + "\n");
                }
            } catch(Exception ex){
            	System.out.println(ex);
                ex.printStackTrace();
            }

      }
        
    }//GEN-LAST:event_jBtnSelectFileActionPerformed

    private void jBtnSendActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBtnSendActionPerformed
        try {
            String message = jTextFieldWrite.getText();
            getClientInterface().getClient().sendToServer("SEND---" + getUsername().trim() + "---string---" + message);
            if (!jMessageArea.getText().equals("")) {
                this.getjMessageArea().setText(getjMessageArea().getText() + getUsername() + " dit :\n" + message + "\n");
            }
            this.getjMessageArea().setText("");
        } catch (Exception ex) {
            System.out.println(ex.toString());
        }
    }//GEN-LAST:event_jBtnSendActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new PrivateChatFrame("Micheal Myers",  null).setVisible(true);
            }
        });


    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jBtnSelectFile;
    private javax.swing.JButton jBtnSend;
    private javax.swing.JTextArea jMessageArea;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextField jTextFieldWrite;
    private javax.swing.JLabel jlblInfo;
    // End of variables declaration//GEN-END:variables

    /**
     * @return the clientInterface
     */
    public ClientInterface getClientInterface() {
        return clientInterface;
    }

    /**
     * @param clientInterface the clientInterface to set
     */
    public void setClientInterface(ClientInterface clientInterface) {
        this.clientInterface = clientInterface;
    }

    /**
     * @return the username
     */
    public String getUsername() {
        return username;
    }

    /**
     * @param username the username to set
     */
    public void setUsername(String username) {
        this.username = username;
    }

    /**
     * This is Bad but not avoidable since ClientInterface uses this frame's
     * message area directly to read from and write to
     * @return the jMessageArea
     */
    public javax.swing.JTextArea getjMessageArea() {
        return jMessageArea;
    }

    /**
     * @param jMessageArea the jMessageArea to set
     */
    public void setjMessageArea(javax.swing.JTextArea jMessageArea) {
        this.jMessageArea = jMessageArea;
    }
}
